<!--
* Created by Sean on 2017/10/2.
* Reset Password
-->
<template>
  <div id="resetPwd" class="reset-pwd">
    <group>
      <x-input :show-clear="false"placeholder="请输入原始登录密码" type="password" v-model="oldPwd" :should-toast-error="false">
        <img slot="label" src="../../../images/icon_lock_blue.png">
      </x-input>
      <x-input :show-clear="false"placeholder="请输入新密码" type="password" v-model="newPwd" ref="newPwd" :is-type="isPwd" :should-toast-error="false">
        <img slot="label" src="../../../images/icon_lock_blue.png">
      </x-input>
      <x-input :show-clear="false"placeholder="请再次输入新密码" class="vux-1px-b" type="password" v-model="newPwd1" :should-toast-error="false">
        <img slot="label" src="../../../images/icon_lock_blue.png">
      </x-input>
      <div class="btn-wrapper"><x-button type="primary" @click.native="resetPwd">确定</x-button></div>
    </group>
  </div>
</template>
<script>
  export default {
    name: 'resetPwd',
    data () {
      return {
        oldPwd: '',
        newPwd: '',
        newPwd1: '',
        isPwd: function (value) {
          return {
            valid: /^\w{6,30}$/g.test(value),
            msg: '密码为数字、字母的组合，不得少于 6 位'
          }
        }
      }
    },
    methods: {
      resetPwd () {
        if (this.oldPwd.trim() === '') {
          this.$vux.toast.text('请输入原始密码', 'middle')
          return false
        }
        if (this.newPwd.trim() === '') {
          this.$vux.toast.text('请输入新密码', 'middle')
          return false
        }
        if (this.newPwd1.trim() === '') {
          this.$vux.toast.text('请再次输入新密码', 'middle')
          return false
        }
        if (this.$refs.newPwd.valid === false) {
          this.$vux.toast.text('密码为数字、字母的组合，不得少于 6 位', 'middle')
          return false
        }
        if (this.newPwd !== this.newPwd1) {
          this.$vux.toast.text('两次密码不一致，请重新输入', 'middle')
          return false
        }
        let userInfo = JSON.parse(localStorage.getItem('userInfo'))
        if (!userInfo) {
          this.$router.replace('/login')
          return false
        }
        this.$axiosPosting(this.$api.resetPwd, {
          mobile: userInfo.mobile,
          oldPwd: this.oldPwd,
          newPwd: this.newPwd
        }).then(json => {
          if (json.code === '0000') {
            let that = this
            this.$vux.alert.show({
              title: '提示',
              content: '密码修改成功',
              onHide () {
                window.localStorage.userInfo = ''
                that.$router.replace('/login')
              }
            })
          } else {
            this.$vux.toast.text(json.msg, 'middle')
          }
        })
      }
    }
  }
</script>
<style lang="less">
  html {
    background: #fff !important;
  }
  .reset-pwd {
    .weui-cells {
      margin-top: 0;
    }
    .vux-x-input {
      padding: .8rem .75rem;
    }
    .btn-wrapper {
      margin-top: 1rem;
      padding: 0 .75rem;
    }
    .weui-cells:after {
      display: none;
    }
  }
</style>
